<?php

    /**
     * This is the model class for table "{{customerloansettings}}".
     *
     * The followings are the available columns in table '{{customerloansettings}}':
     * @property integer $id
     * @property integer $customer_id
     * @property string $customer_code
     * @property string $loan_code
     * @property string $loan_amount
     * @property double $interest
     * @property integer $period
     * @property string $period_type
     * @property integer $days_of_period
     * @property string $paying_amount
     * @property integer $installments
     * @property string $payment_per_installment
     * @property string $last_period_payment
     * @property bit $active
     * @property string $status
     * @property string $createtime
     * @property integer $updatedby_user_id
     * @property string $modifytime
     * @property string $withdraw_amount
     */
    class CustomerLoanSetting extends CActiveRecord {

        /**
         * @return string the associated database table name
         */
        public function tableName() {
            return '{{customerloansettings}}';
        }

        /**
         * @return array validation rules for model attributes.
         */
        public function rules() {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('customer_code, loan_code, loan_amount, interest, period, paying_amount, installments, payment_per_installment, last_period_payment, withdraw_amount', 'required'),
                array('customer_id, period, days_of_period, installments, updatedby_user_id', 'numerical', 'integerOnly' => true),
                array('interest', 'numerical'),
                array('customer_code, loan_code, period_type, status', 'length', 'max' => 20),
                array('loan_amount, withdraw_amount, paying_amount, payment_per_installment, last_period_payment', 'length', 'max' => 15),
                array('loan_amount, withdraw_amount, paying_amount, payment_per_installment, last_period_payment', 'numerical'),
                array('active', 'length', 'max' => 1),
                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('id, customer_id, customer_code, loan_code, loan_amount, interest, period, period_type, days_of_period, paying_amount, withdraw_amount, installments, payment_per_installment, last_period_payment, active, status, createtime, updatedby_user_id, modifytime', 'safe', 'on' => 'search'),
            );
        }

        /**
         * @return array relational rules.
         */
        public function relations() {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'customer' => array(self::BELONGS_TO, 'Customers', 'customer_id',),
                'loanpayment' => array(self::HAS_MANY, 'CustomerLoanPayment', 'loansetting_id'),
                'user' => array(self::BELONGS_TO, 'User', array('updateby_user_id' => 'id'), 'through' => 'customer'),
                //calculate how much owed of the loan
                'owedloanSum' => array(self::STAT, 'CustomerLoanPayment', 'loansetting_id',
                    'select' => 'sum(payment)',
                    'condition' => 'paid_amount = 0',
                ),
                'owedloanCount' => array(self::STAT, 'CustomerLoanPayment', 'loansetting_id',
                    'condition' => 'paid_amount = 0',
                ),
            );
        }

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels() {
            return array(
                'id' => 'ID',
                'customer_id' => t('Customer ID'),
                'customer_code' => t('Customer Code'),
                'loan_code' => t('Loan Code'),
                'loan_amount' => t('Loan Amount'),
                'interest' => t('Interest %'),
                'period' => t('Period'),
                'period_type' => t('Period Type'),
                'days_of_period' => t('Days Of Period'),
                'paying_amount' => t('Paying Amount'),
                'withdraw_amount' => t('Withdraw Amount'),
                'installments' => t('Installments'),
                'payment_per_installment' => t('Payment Per Installment'),
                'last_period_payment' => t('Last Period Payment'),
                'active' => t('Active'),
                'status' => t('Status'),
                'createtime' => t('Created Time'),
                'updatedby_user_id' => t('Updated By User'),
                'modifytime' => t('Modified Time'),
            );
        }

        /**
         * Retrieves a list of models based on the current search/filter conditions.
         *
         * Typical usecase:
         * - Initialize the model fields with values from filter form.
         * - Execute this method to get CActiveDataProvider instance which will filter
         * models according to data in model fields.
         * - Pass data provider to CGridView, CListView or any similar widget.
         *
         * @return CActiveDataProvider the data provider that can return the models
         * based on the search/filter conditions.
         */
        public function search() {
            // @todo Please modify the following code to remove attributes that should not be searched.

            $criteria = new CDbCriteria;

            $criteria->compare('id', $this->id);
            $criteria->compare('customer_id', $this->customer_id);
            $criteria->compare('customer_code', $this->customer_code, true);
            $criteria->compare('loan_code', $this->loan_code, true);
            $criteria->compare('loan_amount', $this->loan_amount, true);
            $criteria->compare('interest', $this->interest);
            $criteria->compare('period', $this->period);
            $criteria->compare('period_type', $this->period_type, true);
            $criteria->compare('days_of_period', $this->days_of_period);
            $criteria->compare('paying_amount', $this->paying_amount, true);
            $criteria->compare('withdraw_amount', $this->withdraw_amount, true);
            $criteria->compare('installments', $this->installments);
            $criteria->compare('payment_per_installment', $this->payment_per_installment, true);
            $criteria->compare('last_period_payment', $this->last_period_payment, true);
            $criteria->compare('active', $this->active, true);
            $criteria->compare('status', $this->status, true);
            $criteria->compare('createtime', $this->createtime, true);
            $criteria->compare('updatedby_user_id', $this->updatedby_user_id);
            $criteria->compare('modifytime', $this->modifytime, true);

            return new CActiveDataProvider($this, array(
                'criteria' => $criteria,
            ));
        }

        /**
         * Returns the static model of the specified AR class.
         * Please note that you should have this exact method in all your CActiveRecord descendants!
         * @param string $className active record class name.
         * @return CustomerLoanSetting the static model class
         */
        public static function model($className = __CLASS__) {
            return parent::model($className);
        }

    }

